define(function (require,exports,modules) {
    let $=require("jquery");
    require("cookie");
    let login=function () {
        $("form").submit(function () {
            $.ajax({
                url:"../../server/login.php",
                type:"post",
                dataType:"json",
                data:$("form").serialize()
            }).then(function (res) {
                console.log(res);
                if(res.status==1){
                    sessionStorage.setItem("userInfo",JSON.stringify(res.data));
                    alert(res.msg);
                    let cookieList=JSON.parse($.cookie("carts")||'[]');
                    var arr=[];
                    cookieList.forEach((ele,index)=>{
                        ele.u_id=res.data.u_id;
                        ele.img=ele.img[0];
                        console.log(ele.newPrice);
                        let p=$.ajax({
                            url:"../../server/addCart.php",
                            type:"post",
                            data:ele
                        });
                        arr.push(p.promise());

                    })
                    Promise.all(arr).then(function (res) {
                        $.cookie("carts","",{expires:-1});
                        location="index.html";
                    })

                }else{
                    alert(res.msg);
                }
            })
            return false;
        })
    }
    exports.login=login;
})